<template>
  <div class="personal-setting">
    <h2>个人设置</h2>

    <!-- 展示用户信息 -->
    <div class="section">
      <el-descriptions title="基本信息" :column="1" border>
        <el-descriptions-item label="用户名">{{ userInfo.username }}</el-descriptions-item>
        <el-descriptions-item label="电话号码">{{ userInfo.phone_number }}</el-descriptions-item>
        <el-descriptions-item label="头像">
          <img
            :src="userInfo.avatar && userInfo.avatar.trim() !== '' ? userInfo.avatar : defaultAvatar"
            class="avatar"
          />
        </el-descriptions-item>
      </el-descriptions>

      <el-button type="primary" icon="el-icon-edit" size="small" @click="editVisible = true">
        编辑信息
      </el-button>
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑个人信息" :visible.sync="editVisible" width="500px">
      <el-form :model="editForm" ref="editForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="editForm.phone_number" />
        </el-form-item>
        <el-form-item label="头像链接">
          <el-input v-model="editForm.avatar" />
        </el-form-item>

        <el-form-item label="默认头像选择">
          <div class="default-avatar-list">
            <img
              v-for="(url, index) in defaultAvatars"
              :key="index"
              :src="url"
              :class="['default-avatar', editForm.avatar === url ? 'selected' : '']"
              @click="selectDefaultAvatar(url)"
            />
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import defaultAvatar from "@/assets/avatar.png";
import avatar1 from "@/assets/avatar1.png";
import avatar2 from "@/assets/avatar2.png";
import avatar3 from "@/assets/avatar3.png";

export default {
  name: "PersonalSetting",
  data() {
    return {
      defaultAvatar,
      defaultAvatars: [avatar1, avatar2, avatar3],
      editVisible: false,
      userInfo: {
        username: "",
        phone_number: "",
        avatar: ""
      },
      editForm: {
        username: "",
        phone_number: "",
        avatar: ""
      }
    };
  },
  created() {
    this.loadUserInfo();
  },
  methods: {
    loadUserInfo() {
      setTimeout(() => {
        this.userInfo = {
          username: "zhangsan",
          phone_number: "13888888888",
          avatar: avatar1
        };
      }, 300);
    },
    openEdit() {
      this.editForm = { ...this.userInfo };
    },
    submitEdit() {
      this.userInfo = { ...this.editForm };
      this.$message.success("修改成功！");
      this.editVisible = false;
    },
    selectDefaultAvatar(url) {
      this.editForm.avatar = url;
    }
  },
  watch: {
    editVisible(val) {
      if (val) {
        this.openEdit();
      }
    }
  }
};
</script>

<style scoped>
.personal-setting {
  padding: 20px;
  max-width: 800px;
}
.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}
.dialog-footer {
  text-align: right;
}
.default-avatar-list {
  display: flex;
  gap: 10px;
}
.default-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  object-fit: cover;
  transition: border-color 0.3s;
}
.default-avatar:hover {
  border-color: #409eff;
}
.default-avatar.selected {
  border-color: #409eff;
}
</style>
